package com.superb.allocation.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import com.superb.common.core.annotation.SuperbDataScope;
import com.superb.common.core.annotation.SuperbRestMapping;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.superb.common.core.model.Result;
import com.superb.common.utils.StringUtils;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.superb.common.database.vo.BatchId;
import com.superb.common.database.vo.PageParams;
import com.superb.allocation.api.entity.AllocationPosition;
import com.superb.allocation.service.AllocationPositionService;

import jakarta.annotation.Resource;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;

/**
 * 行政区划;(allocation_position)表控制层
 * @Author: ajie
 * @CreateTime: 2024-7-1
 */
@SuperbDataScope
@SuperbRestMapping("/allocationPosition")
@Tag(name = "行政区划接口")
@ApiSupport(author = "阿杰：ajie20999@163.com")
public class AllocationPositionController {

    @Resource
    private AllocationPositionService allocationPositionService;

    @PostMapping("query")
    @ApiOperationSupport(order = 1)
    @Operation(summary = "级别查询", description = "分页无效")
    public Result<List<AllocationPosition>> pageQuery(@RequestBody PageParams<AllocationPosition> pageParams) {
        AllocationPosition params = pageParams.getParams();
        LambdaQueryWrapper<AllocationPosition> lqw = new LambdaQueryWrapper<>();
        lqw.eq(StringUtils.isNotBlank(params.getLevel()), AllocationPosition::getLevel, params.getLevel());
        lqw.eq(StringUtils.isNotBlank(params.getParentId()), AllocationPosition::getParentId, params.getParentId());
        lqw.eq(StringUtils.isNotBlank(params.getId()), AllocationPosition::getId, params.getId());
        return Result.success(allocationPositionService.list(lqw));
    }

    @PostMapping("insert")
    @ApiOperationSupport(order = 2)
    @SaCheckPermission("allocation:position:insert")
    @Operation(summary = "新增信息", description = "权限: allocation:position:insert<br>")
    public Result<Boolean> insert(@RequestBody @Validated AllocationPosition allocationPosition) {
        if (allocationPositionService.save(allocationPosition)) {
            return Result.success();
        }
        return Result.error();
    }

    @PostMapping("update")
    @ApiOperationSupport(order = 3)
    @SaCheckPermission("allocation:position:update")
    @Operation(summary = "修改信息", description = "权限: allocation:position:update<br>")
    public Result<Boolean> update(@RequestBody @Validated AllocationPosition allocationPosition) {
        if (allocationPositionService.updateById(allocationPosition)) {
            return Result.success();
        }
        return Result.error();
    }

    @GetMapping("remove/{id}")
    @ApiOperationSupport(order = 5)
    @SaCheckPermission("allocation:position:delete")
    @Operation(summary = "根据id删除", description = "权限: allocation:position:delete<br><font style='color: #1890FF'>假删除，会进入回收站</font>", parameters = {
            @Parameter(name = "id", description = "业务主键", required = true, in = ParameterIn.PATH)
    })
    public Result<Boolean> removeById(@PathVariable String id) {
        if (allocationPositionService.deleteByBatchId(Collections.singletonList(id))) {
            return Result.success();
        }
        return Result.error();
    }
}